package com.gitee.mrj.aiboot.core.sf.generators.type;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.sf.type.AlainBoolean;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class AlainBooleanGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        ObjectNode fieldFormDefinition = node.getSchema();
        AlainBoolean alainField = AnnotatedElementUtils.findMergedAnnotation(field, AlainBoolean.class);
        if (StringUtils.isNotBlank(alainField.defaultValue())) {
            fieldFormDefinition.put("defaultValue", Boolean.parseBoolean(alainField.defaultValue()));
        }
        fieldFormDefinition.put("type", SFSchemaType.BOOLEAN.toString());
        node.getUi().put("widget", SFSchemaType.BOOLEAN.toString());
    }

    @Override
    public Class[] supports() {
        return new Class[]{AlainBoolean.class};
    }
}
